import os
import sqlite3

from Util import Util

class User:
    def __init__(self, id="", username="", password=""):
        if id:
            self.id = id
        self.username = username
        self.password = password

class UserDAO:
    def checkUser(self, username, password):
        result = False
        try:
            sql = "select username,password from user where username='{}'".format(username)
            conn = sqlite3.connect(os.path.join(os.path.abspath("./database"), "data.db"))
            cursor = conn.cursor()
            cursor.execute(sql)
            row = cursor.fetchone()
            if row:
                password_store = row[1]
                if password_store==Util.hash(password):
                # if Util.cert==Util.hash(password):
                    result = True
                else:
                    result = False
            else:
                result = False
            conn.close()
        except Exception as e:
            print("Exception occurs when executing {}".format(sql))
            print(e)

        return result